package com.tkjt.pms.modules.common.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.common.entity.TkDataConfig;
import com.tkjt.pms.modules.budget.common.DataTypeVo;
import com.tkjt.pms.modules.common.mapper.TkDataConfigMapper;
import com.tkjt.pms.modules.common.service.TkDataConfigService;

@Transactional
@Service("tkDataConfigService")
public class TkDataConfigServiceImpl extends ServiceImpl<TkDataConfigMapper, TkDataConfig> implements TkDataConfigService {
	
	@Autowired
	private TkDataConfigMapper tkDataConfigMapper;

	@Override
	public String getDataType(String userName) {
		QueryWrapper<TkDataConfig> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TkDataConfig::getUserName, userName);
		queryWrapper.lambda().eq(TkDataConfig::getIsValid, "1");
		TkDataConfig tkDataConfig = tkDataConfigMapper.selectOne(queryWrapper);
		if (tkDataConfig == null) {
			return "kpi";
		} else {
			return tkDataConfig.getDataType();
		}
	}

	@Override
	public void dataTypeCtrl(LoginUser loginUser, TkDataConfig tkDataConfig) {
		QueryWrapper<TkDataConfig> query = new QueryWrapper<>();
		query.lambda().eq(TkDataConfig::getUserName, loginUser.getUsername());
		query.lambda().eq(TkDataConfig::getIsValid, "1");
		TkDataConfig oldConfig = tkDataConfigMapper.selectOne(query);
		if(oldConfig != null){
			oldConfig.setIsValid("0");
			oldConfig.setUpdateBy(loginUser.getUsername());
			oldConfig.setUpdateDate(new Date());
			tkDataConfigMapper.updateById(oldConfig);
		}
		if (StringUtils.isEmpty(tkDataConfig.getUserName())) {
			tkDataConfig.setUserName(loginUser.getUsername());
		}
		tkDataConfig.setIsValid("1");
		tkDataConfig.setCreateDate(new Date());
		tkDataConfig.setCreateBy(loginUser.getUsername());
		tkDataConfigMapper.insert(tkDataConfig);
	}

	@Override
	public List<DataTypeVo> listDataTypes(String userName) {
		List<DataTypeVo> results = new ArrayList<>();

		QueryWrapper<TkDataConfig> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TkDataConfig::getUserName, userName);
		queryWrapper.lambda().eq(TkDataConfig::getIsValid, "1");
		TkDataConfig tkDataConfig = tkDataConfigMapper.selectOne(queryWrapper);

		if(tkDataConfig == null) {
			DataTypeVo vo = new DataTypeVo();
			vo.setUserName(userName);
			vo.setDataType("kpi");
			vo.setDataTypeName("考核数据");
			vo.setSelected(Boolean.valueOf(true));
			results.add(vo);
			vo = new DataTypeVo();
			vo.setUserName(userName);
			vo.setDataType("budget");
			vo.setDataTypeName("预算数据");
			vo.setSelected(Boolean.valueOf(false));
			results.add(vo);
			return results;
		}
		if("kpi".equals(tkDataConfig.getDataType())){

			DataTypeVo vo = new DataTypeVo();
			vo.setDataType(tkDataConfig.getDataType());
			vo.setDataTypeName("考核数据");
			vo.setSelected(Boolean.valueOf(true));
			results.add(vo);

			vo = new DataTypeVo();
			vo.setUserName(userName);
			vo.setDataType("budget");
			vo.setDataTypeName("预算数据");
			vo.setSelected(Boolean.valueOf(false));
			results.add(vo);
		} else {
			DataTypeVo vo = new DataTypeVo();
			vo.setDataType(tkDataConfig.getDataType());
			vo.setDataTypeName("预算数据");
			vo.setSelected(Boolean.valueOf(true));
			results.add(vo);

			vo = new DataTypeVo();
			vo.setUserName(userName);
			vo.setDataType("kpi");
			vo.setDataTypeName("考核数据");
			vo.setSelected(Boolean.valueOf(false));
			results.add(vo);
		}
		return results;
	}
}
